<?php
namespace app\api\controller;

use hema\map\Driver as Map;
use think\facade\Cache;

/**
 * 首页控制器
 */
class Index extends Controller
{
    /**
     * 获取位置详情
     */
    public function page()
    {
        $banner = [
            0 => [
                'image' => base_url().  '/addons/pt/applet/banner1.png',    
            ],
            1 => [
                'image' => base_url().  '/addons/pt/applet/banner1.png',    
            ]
        ];
        return $this->renderSuccess(compact('banner'));
    }
    /**
     * 获取位置详情
     */
    public function getLocation(string $location)
    {
        //为了避免频繁调用解析接口，造成接口额度数量的浪费，采用缓存机制，30分钟更新一次
        $position = Cache::get('user_position_'.$this->user_id);
        if($position == null){
            $map = new Map;
    		if (!$position = $map->getLocation($location)){
                return $this->renderError($map->getError());
            }
    		Cache::set('user_position_'.$this->user_id,$position,1800);//有效期30分钟
        }
        return $this->renderSuccess($position);
    }
}